package com.mollytask.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.mollytask.MainActivity;
import com.mollytask.R;
import com.mollytask.bean.MyUser;
import com.mollytask.util.DialogUtils;
import com.mollytask.util.RegexUtil;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener, View.OnFocusChangeListener {

    private ImageView mReturnIv;
    private EditText login_et_phone;
    private EditText login_et_password;
    private Button login_btn_login;
    private String phone;
    private Boolean hasPhone = false;
    private Boolean hasPassword = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        findId();
        setListener();

    }

    private void findId() {
        mReturnIv = (ImageView) findViewById(R.id.login_iv_return);
        login_et_phone = (EditText) findViewById(R.id.login_et_phone);
        login_et_password = (EditText) findViewById(R.id.login_et_password);
        login_btn_login = (Button) findViewById(R.id.login_btn_login);

        login_btn_login.setOnClickListener(this);
    }

    private void setListener() {
        mReturnIv.setOnClickListener(this);
        login_et_password.setOnFocusChangeListener(this);
        login_et_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() == 0) {
                    hasPassword = false;
                } else {
                    hasPassword = true;
                }
                if (hasPassword & hasPhone) {
                    login_btn_login.setBackground(getResources().getDrawable(R.drawable.bg_btn_y));
                    login_btn_login.setClickable(true);
                } else {
                    login_btn_login.setBackground(getResources().getDrawable(R.drawable.bg_btn));
                    login_btn_login.setClickable(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        login_et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() == 0) {
                    hasPhone = false;
                } else {
                    hasPhone = true;
                }
                if (hasPassword & hasPhone) {
                    login_btn_login.setBackground(getResources().getDrawable(R.drawable.bg_btn_y));
                    login_btn_login.setClickable(true);
                } else {
                    login_btn_login.setBackground(getResources().getDrawable(R.drawable.bg_btn));
                    login_btn_login.setClickable(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //返回按钮
            case R.id.login_iv_return:
                finish();
                break;
            //登录按钮
            case R.id.login_btn_login:
                String phone = login_et_phone.getText().toString().trim();
                String pwd = login_et_password.getText().toString().trim();
                if (!RegexUtil.checkMobile(phone)) {
                    DialogUtils.showToast(this, "无效的手机号码");
                } else {
                    BmobUser.loginByAccount(phone, pwd, new LogInListener<MyUser>() {
                        @Override
                        public void done(MyUser myUser, BmobException e) {
                            if (e == null) {
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                                WelcomeActivity.mActivity.finish();

                            } else {
                                DialogUtils.showToast(LoginActivity.this, e.getMessage().toString());

                            }
                        }
                    });
                }
                break;
        }
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        phone = login_et_phone.getText().toString().trim();
        switch (view.getId()) {
            case R.id.login_et_password:
                if (b) {
                    if (phone.equals("")) {
                        DialogUtils.showToast(LoginActivity.this, "请输入手机号码");

                    } else {
                        if (!RegexUtil.checkMobile(phone)) {
                            DialogUtils.showToast(LoginActivity.this, "无效的手机号码");

                        }
                    }
                }
                break;
        }
    }
}

